﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;
using System.ComponentModel;
using System.Dynamic;
using FinPlusWCFConnData;
using FinPlusCommon;
using log = Logger.Logger;
using u = FinPlusUtility.Utility;

namespace FinPlusWPFControls
{
    public partial class FinPlusStackPanel : FinPlusControl
    {
        public override string Id { get; set; }
        public override string[] States { get { return new string[] { "Height", "Width", "Left", "Top" }; } }
        public override string[] TestStates { get { return new string[] { "ToString", "ChildrenCount", "IsValid" }; } }
        public override IFinPlusFunctions Model { get { return base.Children(stackPanel.Children); } }
        public override string ModelTest { get { return null; } }
        public override bool IsValid { get; set; }
              
        public int ChildrenCount { get { return stackPanel.Children.Count; } }

        private enum DockControls { FinPlusCashGap, FinPlusGroupGrid, FinPlusParentService, Exception, Default }

        private FinPlusParentService _conn;
        private ObservableCollection<IFinPlusFuncDynamic> _funcs;

        //construct
        public FinPlusStackPanel()
        {
            InitializeComponent();
            IsValid = true;
        }

        //common control interface
        public override void Dock(object control, string dock)
        {
            try
            {
                switch(u.EnumTryParse<DockControls>(control.GetType().Name, DockControls.Default))
                {
                    case DockControls.FinPlusParentService: AddConnection((FinPlusParentService)control);  AddChildren(dock); break;
                    case DockControls.Exception: stackPanel.Children.Add(new FinPlusErrorDisplay((Exception)control)); break;
                    case DockControls.Default: stackPanel.Children.Add((UserControl)control); break;
                }
            }
            catch (Exception e)
            {
                IsValid = log.Log(Logger.Level.Warning, e.Message, e);
                stackPanel.Children.Add(new FinPlusErrorDisplay(e));
            }
        }

        public override void Initialise()
        {

        }

        public override void Dispose()
        {
            try
            {
                _conn.Dispose();
            }
            catch (Exception e)
            {
                log.Log(Logger.Level.Error, Id, Config, e.Message, e);
            }
        }

        public override void CommandRouter(IFinPlusFunction func)
        {
            try
            {
                throw new NotImplementedException();
            }
            catch (Exception e)
            {
                IsValid = log.Log(Logger.Level.Warning, e.Message, e);
            }
        }
        
        //private
        private void AddConnection(FinPlusParentService conn)
        {
            try
            {
                _conn = conn;
                _funcs = _conn.GetFuncsTransposed();
            }
            catch (Exception e)
            {
                log.Log(Logger.Level.Error, Id, Config, e.Message, e);
                stackPanel.Children.Add(new FinPlusErrorDisplay(e));
            }
        }

        private void AddChildren(string dock)
        {
            var controls = dock.Split(',');
            foreach (var func in _funcs)
            {
                foreach (var control in controls)
                {
                    switch(u.EnumTryParse<DockControls>(control, DockControls.Exception))
                    {
                        case DockControls.FinPlusCashGap: AddCashGap(func); break;
                        case DockControls.FinPlusGroupGrid: AddGroupGrid(func); break;
                        default: throw new NotImplementedException();
                    }
                }
            }
        }

        private void AddCashGap(IFinPlusFuncDynamic func)
        {
            try
            {
                var cashGap = new FinPlusGapCashChart();

                object id;
                if (func.TryGetObject("Id", out id))
                    cashGap.Id = cashGap.GetType().Name + id.ToString(); 

                cashGap.AddChart(func);
                stackPanel.Children.Add(cashGap);
            }
            catch (Exception e)
            {
                log.Log(Logger.Level.Error, Id, Config, e.Message, e);
                stackPanel.Children.Add(new FinPlusErrorDisplay(e));
            }
        }

        private void AddGroupGrid(IFinPlusFuncDynamic func)
        {
            try
            {
                var groupGrid = new FinPlusGroupGrid(string.Empty, string.Empty, string.Empty, string.Empty, "True", "False");
                groupGrid.AddFunction(func);

                object id;
                if (func.TryGetObject("Id", out id))
                    groupGrid.Id = groupGrid.GetType().Name + id.ToString(); 

                stackPanel.Children.Add(groupGrid);
            }
            catch (Exception e)
            {
                log.Log(Logger.Level.Error, Id, Config, e.Message, e);
                stackPanel.Children.Add(new FinPlusErrorDisplay(e));
            }
        }
    }
}
